<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
	String user_name = (session
			.getValue("SPRING_SECURITY_LAST_USERNAME")).toString();
%>
<html>
	<head>
		<title>有功总电能量曲线</title>
		<link id=style href="../styles/theme/blue/frame.css" type="text/css"
			rel="stylesheet" />
		<style type="text/css">
.divLinechart {
	width: 100%;
	margin: 5px;
}
</style>
		<script type='text/javascript' src='../ajax-pushlet-client.js'></script>
		<script type='text/javascript' src='../js/jquery.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/jquery.blockUI.js'></script>
		<script type="text/javascript"
			src="<%=request.getContextPath()%>/js/highcharts.js"></script>
		<script type="text/javascript"
			src="<%=request.getContextPath()%>/js/highChartsTheme.js"></script>
		<script type="text/javascript"
			src="<%=request.getContextPath()%>/scripts/curveCommon.js"></script>
		<script>
function onData(event){
	var activePowerEnergyData = event.get("activePowerEnergyData");
	if(event.get("pnfn") != "" && event.get("pnfn") != undefined && event.get("pnfn") == "0101010c") { // 处理正向有功总电能量曲线返回数据
    	$("#fActivePowerEnergyData").val(activePowerEnergyData);
    }
    else if(event.get("pnfn") != "" && event.get("pnfn") != undefined && event.get("pnfn") == "0101040c") {// 处理反向有功总电能量曲线返回数据
    	$("#rActivePowerEnergyData").val(activePowerEnergyData);
    }
    
    // 如果响应都返回后，根据数据绘制曲线图
    if ($("#fActivePowerEnergyData").val() != "" && $("#rActivePowerEnergyData").val() != "" && $("#fActivePowerEnergyData").val() != undefined  && $("#rActivePowerEnergyData").val() != undefined ) {
	    
	    // 准备正向、反向有功总电能量
	    var fActivePowerEnergyDataArr = $("#fActivePowerEnergyData").val().split(":");
	    var rActivePowerEnergyDataArr = $("#rActivePowerEnergyData").val().split(":");
	    var startTime = event.get("startTime");
		
		var fActivePowerEnergyDataArrDouble = new Array();
		var rActivePowerEnergyDataArrDouble = new Array();
		for (var i=0; i<fActivePowerEnergyDataArr.length; i++) {
			fActivePowerEnergyDataArrDouble[i] = parseFloat(fActivePowerEnergyDataArr[i]);
			rActivePowerEnergyDataArrDouble[i] = parseFloat(rActivePowerEnergyDataArr[i]);
		}
		
		// 修正各曲线的最大值
		var fCorrectYAxisMax = correctYAxisMax(fActivePowerEnergyDataArrDouble, 2/3);
		var rCorrectYAxisMax = correctYAxisMax(rActivePowerEnergyDataArrDouble, 2/3);
		
		var maxVal = -999999999;
		maxVal = fCorrectYAxisMax > maxVal ? fCorrectYAxisMax : maxVal;
		maxVal = rCorrectYAxisMax > maxVal ? rCorrectYAxisMax : maxVal;
		maxVal = maxVal > 0.0 ? maxVal : 10;
		
		// 准备时间
		var time = new Array();
		time[0] = startTime;
		var date= new Date(Date.parse(startTime.replace(/-/g,   "/"))); 
		for (var i=1; i<fActivePowerEnergyDataArr.length; i++) {
		
			var date2Minsec = date.getTime();
		
			var addFifteen = 1000 * 60 * 15;
			date2Minsec += addFifteen;
			date = new Date(date2Minsec);
			time[i]= date2Str(date);
		}
		
		// 创建折线图，生成后放置在id为container的div中
		titleText = "有功总电能量曲线";
		colors = otherColor;
		yAxisText = "有功总电能量(kWh)";
		minVal = 0;
		unit = "kWh";
		seriesVal = [{
			name: '正向有功总电能量',
			data: fActivePowerEnergyDataArrDouble
		}, {
			name: '反向有功总电能量',
			data: rActivePowerEnergyDataArrDouble
		}];
		var container = parent.window.frames["lineChart"].document.getElementById("container");
		var chart = showChart(container, titleText, startTime, colors, time, yAxisText, maxVal, minVal, unit, seriesVal);
    }
}

// pushlet ajax开始，监听/source/activePowerEnergy的报文
PL._init('<%=user_name%>');
PL.joinListen('/source/activePowerEnergy');
</script>
	</head>
	<body style="overflow-y: hidden; overflow-x: hidden">
		<input type="hidden" name="fActivePowerEnergyData"
			id="fActivePowerEnergyData" />
		<input type="hidden" name="rActivePowerEnergyData"
			id="rActivePowerEnergyData" />
		<div class="divLinechart" id="container">
		</div>
	</body>
</html>
